package org.linlinjava.litemall.wx.service;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.core.qcode.QCodeService;
import org.linlinjava.litemall.core.util.JacksonUtil;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.ILiquormallLiquor;
import org.linlinjava.litemall.db.service.ILiquormallLiquorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class WxLiquorService {
    private final Log logger = LogFactory.getLog(WxLiquorService.class);

    @Autowired
    ILiquormallLiquorService iLiquormallLiquorService;

    @Autowired
    private QCodeService qCodeService;

    public Object wxAdd (Integer userId, String body) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }
        if (body == null) {
            return ResponseUtil.badArgument();
        }
        String title = JacksonUtil.parseString(body, "title");
        String userType = JacksonUtil.parseString(body, "userType");
        String liquorType = JacksonUtil.parseString(body, "liquorType");
        Integer numberOf = JacksonUtil.parseInteger(body, "numberOf");
        String age = JacksonUtil.parseString(body, "age");
        String price = JacksonUtil.parseString(body, "price");
        String province = JacksonUtil.parseString(body, "province");
        String city = JacksonUtil.parseString(body, "city");
        String addressDetail = JacksonUtil.parseString(body, "addressDetail");
        String link = JacksonUtil.parseString(body, "link");
        String url = JacksonUtil.parseString(body, "url");
        String phone = JacksonUtil.parseString(body, "phone");
        String wxno = JacksonUtil.parseString(body, "wxno");

        ILiquormallLiquor iLiquormallLiquor = new ILiquormallLiquor();
        iLiquormallLiquor.setUserId(userId);
        iLiquormallLiquor.setTitle(title);
        iLiquormallLiquor.setUserType(userType);
        iLiquormallLiquor.setLiquorType(liquorType);
        iLiquormallLiquor.setNumberOf(numberOf);
        iLiquormallLiquor.setAge(age);
        iLiquormallLiquor.setPrice(price);
        iLiquormallLiquor.setProvince(province);
        iLiquormallLiquor.setCity(city);
        iLiquormallLiquor.setAddressDetail(addressDetail);
        iLiquormallLiquor.setLink(link);
        iLiquormallLiquor.setUrl(url);
        iLiquormallLiquor.setPhone(phone);
        iLiquormallLiquor.setWxno(wxno);

        int id = iLiquormallLiquorService.add(iLiquormallLiquor);
        iLiquormallLiquor.setId(id);

        if (id >0) {
            //将生成的分享图片地址写入数据库
            String shareUrl = qCodeService.createGoodShareImage(iLiquormallLiquor.getId().toString(), iLiquormallLiquor.getUrl(), iLiquormallLiquor.getTitle());
            if (!StringUtils.isEmpty(shareUrl)) {
                iLiquormallLiquor.setLink(shareUrl);
                iLiquormallLiquorService.update(iLiquormallLiquor);
            }
        }
        return ResponseUtil.ok();
    }
}
